package com.qiniuyun.backai.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 分页结果封装类
 * 
 * @author 王程楠
 * @param <T> 分页数据类型
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "分页结果封装")
public class PageResult<T> {
    
    @Schema(description = "分页数据列表", example = "[{\"id\":1,\"name\":\"示例数据\"}]")
    private List<T> content;
    
    @Schema(description = "当前页码", example = "1", minimum = "1")
    private Integer page;
    
    @Schema(description = "每页条数", example = "10", minimum = "1")
    private Integer size;
    
    @Schema(description = "总记录数", example = "100", minimum = "0")
    private Long totalElements;
    
    @Schema(description = "总页数", example = "10", minimum = "0")
    private Long totalPages;
    
    /**
     * 构造函数 - 自动计算总页数
     */
    public PageResult(List<T> content, Integer page, Integer size, Long totalElements) {
        this.content = content;
        this.page = page;
        this.size = size;
        this.totalElements = totalElements;
        this.totalPages = calculateTotalPages(totalElements, size);
    }
    
    /**
     * 静态工厂方法 - 创建分页结果
     */
    public static <T> PageResult<T> of(List<T> content, Integer page, Integer size, Long totalElements) {
        return new PageResult<>(content, page, size, totalElements);
    }
    
    /**
     * 静态工厂方法 - 创建空分页结果
     */
    public static <T> PageResult<T> empty(Integer page, Integer size) {
        return new PageResult<>(List.of(), page, size, 0L, 0L);
    }
    
    /**
     * 计算总页数
     */
    private static Long calculateTotalPages(Long totalElements, Integer size) {
        if (totalElements == null || size == null || size <= 0) {
            return 0L;
        }
        return (totalElements + size - 1) / size;
    }
    
    /**
     * 判断是否为第一页
     */
    @JsonIgnore
    public boolean isFirst() {
        return page != null && page <= 1;
    }
    
    /**
     * 判断是否为最后一页
     */
    @JsonIgnore
    public boolean isLast() {
        return page != null && totalPages != null && page.longValue() >= totalPages;
    }
    
    /**
     * 判断是否有下一页
     */
    @JsonIgnore
    public boolean hasNext() {
        return !isLast();
    }
    
    /**
     * 判断是否有上一页
     */
    @JsonIgnore
    public boolean hasPrevious() {
        return !isFirst();
    }
    
    /**
     * 获取下一页页码
     */
    @JsonIgnore
    public Integer getNextPage() {
        return hasNext() ? page + 1 : null;
    }
    
    /**
     * 获取上一页页码
     */
    @JsonIgnore
    public Integer getPreviousPage() {
        return hasPrevious() ? page - 1 : null;
    }
    
    /**
     * 判断是否为空分页
     */
    @JsonIgnore
    public boolean isEmpty() {
        return content == null || content.isEmpty();
    }
    
    /**
     * 获取当前页数据条数
     */
    @JsonIgnore
    public int getNumberOfElements() {
        return content != null ? content.size() : 0;
    }
    
    /**
     * 重写setTotalElements方法以自动重新计算总页数
     */
    public void setTotalElements(Long totalElements) {
        this.totalElements = totalElements;
        // 重新计算总页数
        this.totalPages = calculateTotalPages(totalElements, this.size);
    }
}